package com.zbkj.crmeb.task.product;

import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.chant.service.ChannelActivityService;
import com.zbkj.crmeb.marketing.service.StoreCouponService;
import com.zbkj.crmeb.member.service.IMerchantMemberBenefitsService;
import com.zbkj.crmeb.merchant.api.service.FrontCouponUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author
 * @projectName 优惠券状态
 * @date
 */
@Component
@Configuration
public class CouponUserStatusStock {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(CouponUserStatusStock.class);

    @Value("${switch.task.CouponUserStatusStock:true}")
    Boolean runAble = true;

    @Autowired
    private FrontCouponUserService frontCouponUserService;
    @Autowired
    private IMerchantMemberBenefitsService merchantMemberBenefitsService;
    @Autowired
    private ChannelActivityService channelActivityService;
    @Autowired
    private StoreCouponService storeCouponService;

    @XxlJob("couponUserStatusStock")
    public void init(){
        if(!runAble){
            return;
        }
        try {
            frontCouponUserService.syncCouponUserStatus();
        }catch (Exception e){
            logger.error("CouponUserStatusStock.task error ! e: " ,e);
        }
    }

    /**
     * 优惠券定时发放
     */
    @XxlJob("distributeCouponsRegularly")
    public void distributeCouponsRegularly(){
        logger.info("--------------- 执行优惠券定时发放任务开始 ---------------");
        long start = System.currentTimeMillis();
        merchantMemberBenefitsService.distributeCouponsRegularly();
        storeCouponService.bindCouponBySystem();
        logger.info("--------------- 执行优惠券定时发放任务完成, 耗时: {}秒 ---------------",(System.currentTimeMillis()-start)/1000);
    }

    /**
     * 定时执行限时活动商品下架
     */
    @XxlJob("limitedTimeEventProductOff")
    public void limitedTimeEventProductOff(){
        logger.info("--------------- 定时执行限时活动商品下架任务开始 ---------------");
        long start = System.currentTimeMillis();
        channelActivityService.limitedTimeEventProductOff();
        //merchantMemberBenefitsService.distributeCouponsRegularly();
        logger.info("--------------- 定时执行限时活动商品下架任务完成, 耗时: {}秒 ---------------",(System.currentTimeMillis()-start)/1000);
    }
}
